package tlias.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tlias.annotation.Crud;
import tlias.pojo.*;
import tlias.pojo.ClazzVO;
import tlias.service.ClazzService;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result<PageResult<ClazzVO>> list(ClazzQueryParam param) {
        log.info("分页查询班级，参数：{}", param);
        PageResult<ClazzVO> result = clazzService.list(param);
        log.info("返回结果：{}", result);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Crud
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级，id：{}", id);
        clazzService.delete(new Integer[]{id});
        return Result.success();
    }

    @PostMapping
    @Crud
    public Result add(@RequestBody Clazz clazz) {
        log.info("新增班级，班级信息：{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级信息：{}", id);
        return Result.success(clazzService.getById(id));
    }

    @PutMapping
    @Crud
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<ClazzVO>> list() {
        log.info("查询所有班级信息");
        List<ClazzVO> clazzList = clazzService.listAll();
        return Result.success(clazzList);
    }
}